package com.lancoo.znbkxx.teacher.ui.activity

import android.annotation.SuppressLint
import android.os.Handler
import android.view.View
import android.webkit.*
import android.webkit.WebView.setWebContentsDebuggingEnabled
import com.lancoo.cpbase.authentication.base.CurrentUser
import com.lancoo.znbkxx.teacher.Ext
import com.lancoo.znbkxx.teacher.Ext.Companion.ANALYSIS_URL
import com.lancoo.znbkxx.teacher.Ext.Companion.COURSE_CLASSID
import com.lancoo.znbkxx.teacher.Ext.Companion.GLOBAL_GRADE
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.base.BaseVMActivity
import com.lancoo.znbkxx.teacher.databinding.ActivityAnalysisBinding
import com.lancoo.znbkxx.teacher.viewmodel.UserInfoViewModel

/**
 * @author : Admin
 * @date   : 2019/11/7 11:05
 * @desc   :
 */
class AnalysisActivity : BaseVMActivity<UserInfoViewModel, ActivityAnalysisBinding>(),
    View.OnClickListener {
    override fun providerVMClass(): Class<UserInfoViewModel>? {
        return UserInfoViewModel::class.java
    }

    override fun setLayoutID(): Int {
        return R.layout.activity_analysis
    }

    @SuppressLint("SetJavaScriptEnabled")
    override fun initView() {
        binding.tvTitle.text = getString(R.string.analysis)
        binding.sbtnBack.setOnClickListener(this)
        binding.wv.apply {
            settings.javaScriptEnabled = true
            settings.useWideViewPort = true
            settings.layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS
            settings.loadWithOverviewMode = true
            addJavascriptInterface(JS(), "Android")
            webChromeClient = WebChromeClient()
            webViewClient = WebViewClient()
            var subjectID = ""
            val juniorList = arrayOf("K6","K7", "K8", "K9")
            val seniorList = arrayOf("K10", "K11", "K12")
            if (juniorList.contains(GLOBAL_GRADE)) {
                subjectID = "B"
            }
            if (seniorList.contains(GLOBAL_GRADE)) {
                subjectID = "C"
            }
            loadUrl("${ANALYSIS_URL}KSFX/ClassReview/ReviewIndex.aspx?SchoolID=${CurrentUser.getSchoolID()}&StageNo=$subjectID&CourseClassID=$COURSE_CLASSID&SystemType=2")
        }
    }

    override fun onBackPressed() {
        if (binding.wv.canGoBack()) {
            binding.wv.goBack()
            binding.tvTitle.text = "班级复习效果"
        } else {
            finish()
        }
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.sbtn_back -> if (binding.wv.canGoBack()) {
                binding.wv.goBack()
                binding.tvTitle.text = "班级复习效果"
            } else {
                finish()
            }
        }
    }

    internal inner class JS {
        @JavascriptInterface
        fun getStuScoreDetail(id: String) {
            runOnUiThread {
                binding.wv.loadUrl("${ANALYSIS_URL}KSFX/ClassReview/ReviewWindow.aspx?SchoolID=${CurrentUser.getSchoolID()}&StageNo=C&StuID=$id")
                binding.tvTitle.text = "个人复习效果"
            }
        }
    }
}